package com.showcai.controller.watchSet;

import com.github.pagehelper.PageInfo;
import com.showcai.controller.base.BaseController;
import com.showcai.def.Constant;
import com.showcai.module.DevicIccRelative;
import com.showcai.myRunTime.MyRunTimeExcption;
import com.showcai.result.ResultObj;
import com.showcai.result.ReturnCode;
import com.showcai.service.DevicIccRelativeService;
import com.showcai.tools.PushTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Baggio on 2017-7-3.
 */
@RequestMapping("/watchSet")
@Controller
public class WathSetContorller extends BaseController {

    @Autowired
    DevicIccRelativeService devicIccRelativeService;


    @RequestMapping(value = "/watchSetList")
    public ModelAndView versionsList(Integer page, Integer pageSize, String devicId) {
        ModelAndView mov = new ModelAndView();
        mov.setViewName("/watchSet/watchSetList");
        if (page == null || page == 0) {
            page = Constant.PAGENUM;
        }
        // 设置每页记录数
        if (pageSize == null || pageSize == 0) {
            pageSize = Constant.PAGESIZENum;
        }
        Map<String, String> map = new HashMap<>();
        map.put("page", String.valueOf(page));
        map.put("pageSize", String.valueOf(pageSize));
        map.put("devicId", devicId);
        PageInfo<DevicIccRelative> list = devicIccRelativeService.selectDevicIccRelativePageInfoList(map);
        mov.addObject("pageInfo", list);
        return mov;
    }


    @RequestMapping(value = "/modifyStatus")
    @ResponseBody
    public String modifyStatus(Long tid, Byte status) {
        ResultObj resultObj = new ResultObj();
        try {
            DevicIccRelative devicIccRelative = devicIccRelativeService.modifyDevicIccRelativeStatus(tid, status);

            String body = "{\"type\":3,\"devicId\":\"" + devicIccRelative.getDevicId() + "\"}";
            logger.info(body);
            PushTools.push2DevicService(devicIccRelative.getDevicId(), body);
        } catch (MyRunTimeExcption myRunTimeExcption) {
            myRunTimeExcption.printStackTrace();
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(myRunTimeExcption.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
        }

        return resultObj.toJson();
    }
}
